<template>
    <h2>用户投资赎回</h2>
    <table class="table table-bordered">
        <tr>
            <td>项目名称</td>
            <td>
                {{ data.I_Name }}
            </td>
        </tr>
        <tr>
            <td>项目投资总金额</td>
            <td>
                {{ data.I_Budget }}
            </td>
        </tr>
        <tr>
            <td>用户已投金额</td>
            <td>
                {{ data.UI_Money }}
            </td>
        </tr>
        <tr>
            <td>赎回金额</td>
            <td>
                <input type="text" v-model="URmoney">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="确认赎回" class="btn btn-success" @click="saveRedeemInfo">
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

const data = ref({
    I_Budget: 0,
    I_Id: 0,
    I_Name: '',
    I_Receive: 0,
    UI_Money: 0

})
const URmoney = ref('');

onMounted(() => {
    loadData();
})
//加载数据
const loadData = () => {
    axios.get('https://localhost:44387/api/Invests/ShowUserInvestDetail', {
        params: {
            UI_Id: route.query.UI_Id
        }
    })
        .then(res => {
            console.log(res.data)
            data.value = res.data[0]
        })
        .catch(err => {
            console.log(err)
        })
}
const saveRedeemInfo = () => {
    axios.post('https://localhost:44387/api/Invests/AddUserInvestRedeemInfo', {
        UIR_Money: URmoney.value,
        UI_Id: route.query.UI_Id
    })
        .then(res => {
            console.log(res.data)
            if (res.data > 0) {
                alert('用户投资赎回成功')
                router.push('/UserInvestRedeem')
            } else {
                alert('用户投资赎回失败')
            }
            router.push('/UserInvestRedeem')
        })
        .catch(err => {
            console.log(err)
        })
}
</script>
